package com.dev;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import com.log;
public abstract class BaseIndentEditor extends EditText{
	final static int defaultbgcolor=0x55dec999;
	final MainActivity ma;
	int selectionStart;
	int selectionEnd;
	int lineHeight;
	int currentLineHighlight=0x77de88cf;
	int textColor=0xff000000;
	int tablen;
	int selectionbgcolor=0x77287730;
	int maxTextSize;
	int minTextSize;
	int fingerMovedLim;
	int handleFitEdgeLim;
	int scrollX;
	int scrollY;
	int currentbgcolor;
	int scrollXMaximum;
	int scrollYMaximum;
	String tabWidth="汉字";
	volatile boolean showCursor;
	volatile boolean keepCursorOnShow=false;
	boolean canShowSoftInput=true;
	boolean canEdit=true;
	boolean showSpaceChar;
	boolean showEnterChar;
	boolean showTabChar;
	boolean showZeroWidthChar;
	Paint handle=new Paint();
	InputMethodManager imm;
	volatile TurnCursorVisibility tcv=new TurnCursorVisibility();
	TextPaint textPaint=new TextPaint();
	public BaseIndentEditor(MainActivity ma){
		super(ma);
		this.ma=ma;
		imm=(InputMethodManager)ma.getSystemService(ma.INPUT_METHOD_SERVICE);
		maxTextSize=(int)(35*ma.dp);
		minTextSize=(int)(5*ma.dp);
		fingerMovedLim=(int)(4*ma.dp);
		handleFitEdgeLim=(int)(40*ma.dp);
		handle=new Paint();
		handle.setColor(0xff777f00);
	}
	public abstract void letTextSize(int textSize);
	public abstract void letSelection(int a,int b);
	public abstract void letSelection(int a);
	public abstract void moveSelection(int a,int b);
	public void commitText(String s){}
	@Override
	protected void onVisibilityChanged(View changedView,int visibility){
		super.onVisibilityChanged(changedView,visibility);
		if(visibility==View.VISIBLE){
			if(hasFocus()){
				tcv.finished=true;
				tcv=new TurnCursorVisibility();
				tcv.start();
			}else{
				tcv.finished=true;
				tcv.interrupt();
				showCursor=true;
				invalidate();
			}
		}else{
			tcv.finished=true;
			tcv.interrupt();
		}
	}
	@Override
	protected void onFocusChanged(boolean gainFocus,int direction,Rect previouslyFocusedRect){
		super.onFocusChanged(gainFocus,direction,previouslyFocusedRect);
		if(hasFocus()){
			tcv.finished=true;
			tcv=new TurnCursorVisibility();
			tcv.start();
		}else{
			tcv.finished=true;
			if(!showCursor){
				showCursor=true;
				invalidate();
			}
		}
	}
	class TurnCursorVisibility extends Thread{
		volatile boolean againWait=false;
		volatile boolean finished=false;
		@Override
		public void start(){
			super.start();
			//log.alert("2333");
		}
		@Override
		public synchronized void run(){
			super.run();
			while(!finished){
				try{
					//0log.alert("2333");
					if(tcv!=this)return;
					if(finished)return;
					sleep(500);
					if(tcv!=this)return;
					if(finished)return;
					if(!isShown())return;
					if(againWait){
						againWait=false;
						continue;
					}else if(keepCursorOnShow){
						if(!showCursor){showCursor=true;invalidate();}
					}else{
						showCursor=!showCursor;
						postInvalidate();
					}
				}catch(InterruptedException e){
				}catch(Exception e){
					log.alert("在控制光标的周期显隐时",e.toString());
				}
			}
		}
	}
}
